// [[ OnflowGenerator File UUID: c32f8add5b6349c2b31ba233b46e342c  ]]
package com.itdct.onflow.system.role.service

import com.itdct.onflow.core.constant.CommonString
import com.itdct.onflow.core.constant.ReturnCode
import com.itdct.onflow.core.extend.*
import com.itdct.onflow.core.vo.KeyLabelValueVo
import com.itdct.onflow.core.vo.RespPageVo
import com.itdct.onflow.core.vo.RespVo
import com.itdct.onflow.system.common.base.BaseSysService
import com.itdct.onflow.system.common.context.Context
import com.itdct.onflow.system.common.extend.dict
import com.itdct.onflow.system.role.entity.SysRoleAccountEntity
import com.itdct.onflow.system.role.mapper.SysRoleAccountMapper
import com.itdct.onflow.system.role.qo.SysRoleAccountEditQo
import com.itdct.onflow.system.role.qo.SysRoleAccountSearchQo
import com.itdct.onflow.system.role.vo.SysRoleAccountDetailVo
import com.itdct.onflow.system.role.vo.SysRoleAccountListVo
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

/**
 * @author DCTANT
 * @date 2025-09-03 15:53:01
 * @version 1.0
 * @description 角色账号关联服务
 */
@Service
class SysRoleAccountService(
    val sysRoleAccountMapper: SysRoleAccountMapper
) : BaseSysService() {

    @Transactional(rollbackFor = [Exception::class])
    fun add(qo: SysRoleAccountEditQo, context: Context): RespVo<Long> {
        val addEntity = qo.toEntity(SysRoleAccountEntity::class.java)
        val id = sysRoleAccountMapper.insertEx(addEntity)
        return success(id)
    }

    @Transactional(rollbackFor = [Exception::class])
    fun update(qo: SysRoleAccountEditQo, context: Context): RespVo<Unit> {
        val updateEntity = sysRoleAccountMapper.selectByIdEx(qo.id)
        qo.injectValue(updateEntity)
        sysRoleAccountMapper.updateEx(updateEntity)
        return success()
    }

    fun delete(qo: SysRoleAccountEditQo, context: Context): RespVo<Unit> {
        val ids = qo.ids ?: fail(ReturnCode.LACK_PARAM, CommonString.REQUIRE_DELETE_ID_LIST)
        val deleteEntities = sysRoleAccountMapper.selectByIdsEx(ids)
        sysRoleAccountMapper.deleteBatchEx(deleteEntities)
        return success()
    }

    fun detail(qo: SysRoleAccountSearchQo, context: Context): RespVo<SysRoleAccountDetailVo> {
        val id = qo.id ?: fail(ReturnCode.LACK_PARAM, CommonString.REQUIRE_ID)
        val vo = sysRoleAccountMapper.findById(id) ?: fail(CommonString.ENTITY_NOT_FOUND_USAGE("角色账号关联", id))
        return success(vo.dict())
    }

    fun page(qo: SysRoleAccountSearchQo, context: Context): RespPageVo<SysRoleAccountListVo> {
        startPage(context)
        val pageData = sysRoleAccountMapper.findByList(qo)
        return successPage(pageData.dict())
    }

// [[ OnflowGenerator Range (- selectFunctions -) Start ]]

    fun selectRoleIdList(qo: SysRoleAccountSearchQo, context: Context): RespVo<List<KeyLabelValueVo>> {
        val vos = sysRoleAccountMapper.selectRoleIdList(qo)
        return success(vos)
    }

    fun selectAccountIdList(qo: SysRoleAccountSearchQo, context: Context): RespVo<List<KeyLabelValueVo>> {
        val vos = sysRoleAccountMapper.selectAccountIdList(qo)
        return success(vos)
    }
// [[ OnflowGenerator Range (- selectFunctions -) End ]]
}